//
//  XCNewMVViewController.m
//  SoundOfNature
//
//  Created by 梁新昌 on 16/3/17.
//  Copyright © 2016年 梁新昌. All rights reserved.
//

#import "XCNewMVViewController.h"
#import "XCAFNetTool.h"
#import "XCUrlManger.h"
#import "OnlineAVplay.h"
#import "XCNewMVModel.h"
#import "XCSingleBottumView.h"
#import "XCShareMusicTableViewCell.h"
@interface XCNewMVViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, copy) NSString *value;
@property (nonatomic, retain) NSMutableArray *dataSource;
@property (nonatomic, assign) BOOL MVIsPlaying;
@property (nonatomic, retain) UIButton *playButton;
@property (nonatomic, retain) UISlider *slider;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, retain) UIView *playerView;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIButton *changerBigButton;
@property (nonatomic, retain) AVPlayerLayer *playerLayer;
@property (nonatomic, assign) BOOL isChangeBig;

@end

@implementation XCNewMVViewController

- (instancetype)initWithValue:(NSString *)value WithIndex:(NSInteger) index
{
    self = [super init];
    if (self) {
        self.value = value;
        self.index = index;
    }
    return self;
}
- (void)dealloc {
    
    _tableView.dataSource = nil;
    _tableView.delegate = nil;
    
    [_value release];
    [_dataSource release];
    [_slider release];
    [_playButton release];
    [_changerBigButton release];
    [_tableView release];
    [_playerView release];
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated {
    self.isChangeBig = NO;
    [[OnlineAVplay shareAVPlayer] pause];
    [OnlineAVplay shareAVPlayer].isPlay = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [[OnlineAVplay shareAVPlayer] pause];
    [OnlineAVplay shareAVPlayer].isPlay = NO;
    [self NewMVNetRequest];
    [self AVPlayerBuild];
    [self MVTableViewBuild];
    self.view.backgroundColor = [UIColor colorWithRed:0.823 green:0.950 blue:1.000 alpha:1.000];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changeSlider) userInfo:nil repeats:YES];

}
- (void)NewMVNetRequest {
    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    [XCAFNetTool GETNetWithUrl:[XCUrlManger NewMVNetRequest:self.value] body:nil headerFile:nil response:XCJSON success:^(id result) {
        
        for (NSDictionary *dic in result[@"data"]) {
            XCNewMVModel * newMVModel = [[XCNewMVModel alloc] initWithDataSource:dic];
            [self.dataSource addObject:newMVModel];
            [newMVModel release];
        }
        XCNewMVModel *newMVModel = self.dataSource[self.index];
        XCNewMVListModel *MVListModel = newMVModel.mvList[0];
        [[OnlineAVplay shareAVPlayer] playWithUrl:MVListModel.url];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
//        NSLog(@"网络错误");

        
    }];
}
- (void)AVPlayerBuild {
//    NSLog(@"%ld", (long)self.index);
    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:[OnlineAVplay shareAVPlayer]];
    self.playerLayer.frame = CGRectMake(0, 0, wide, high / 2);
//    self.playerLayer.backgroundColor = [[UIColor colorWithWhite:0.829 alpha:1.000] CGColor];
//    self.playerLayer.backgroundColor = [[UIColor whiteColor] CGColor];
    
    
//    旋转至全屏
//    playerLayer.transform = CATransform3DMakeRotation(M_PI_2, 0, 0, 1);
//    设置显示图层视频的高和宽比自动适应播放窗口的大小
    self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    
    
    self.playerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, wide, high / 2)];
    
    
    self.playButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.playButton.frame = CGRectMake(10, high / 2 - 30, 30, 30);
    
    self.changerBigButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.changerBigButton.frame = CGRectMake(wide - 40, high / 2 - 30, 30, 30);
    [self.changerBigButton setBackgroundImage:[[UIImage imageNamed:@"Image-changeBig"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [self.changerBigButton addTarget:self action:@selector(changeBigButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(wide / 5, high / 2 - 25, wide * 3 / 5, 20)];

    [self.slider setThumbImage:[UIImage imageNamed:@"Image-slider"] forState:UIControlStateNormal];
    [self.slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    [self.playButton addTarget:self action:@selector(playButtonACtion) forControlEvents:UIControlEventTouchUpInside];
    
    if ([OnlineAVplay shareAVPlayer].isPlay) {
        [self.playButton setBackgroundImage:[[UIImage imageNamed:@"Image-MVPause"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [OnlineAVplay shareAVPlayer].isPlay = YES;
    } else {
        [self.playButton setBackgroundImage:[[UIImage imageNamed:@"Image-MVPlay"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [OnlineAVplay shareAVPlayer].isPlay = NO;
    }

//    _playerView.backgroundColor = [UIColor whiteColor];
    
    UIButton *leftBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBarButton.frame = CGRectMake(10, 25, 30, 30);
    [leftBarButton setImage:[UIImage imageNamed:@"Image-back"]forState:UIControlStateNormal];
    [leftBarButton addTarget:self action:@selector(leftBarButtonItemAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.playerView.layer addSublayer:self.playerLayer];
    
    [_playerView addSubview:self.slider];
    [_playerView addSubview:self.changerBigButton];
    [_playerView addSubview:self.playButton];
    [_playerView addSubview:leftBarButton];
    
  
    [self.view addSubview:self.playerView];
    [_playerView release];
    
//    设置音量
    [OnlineAVplay shareAVPlayer].volume = 1.0f;
 
}

- (void)sliderAction: (UISlider *)slider{
    //    跳转到的事件
    //    每秒多少帧(fps)
    
    if (self.slider.value == 1) {
        self.slider = 0;
    } else {
        [[OnlineAVplay shareAVPlayer] seekToTime:CMTimeMakeWithSeconds([OnlineAVplay shareAVPlayer].currentItem.duration.value / [OnlineAVplay shareAVPlayer].currentItem.duration.timescale * self.slider.value, [OnlineAVplay shareAVPlayer].currentTime.timescale) completionHandler:^(BOOL finished) {
            
        }];
    }

    

}
- (void)changeSlider {
        if ([OnlineAVplay shareAVPlayer].currentItem.duration.value) {
    self.slider.value = CMTimeGetSeconds([OnlineAVplay shareAVPlayer].currentItem.currentTime) / CMTimeGetSeconds([OnlineAVplay shareAVPlayer].currentItem.duration);
        }
}

- (void)playButtonACtion {
    if ([OnlineAVplay shareAVPlayer].isPlay) {
        [[OnlineAVplay shareAVPlayer] pause];
        [self.playButton setBackgroundImage:[[UIImage imageNamed:@"Image-MVPlay"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [OnlineAVplay shareAVPlayer].isPlay = NO;
        
    } else {
        [[OnlineAVplay shareAVPlayer] play];
        [self.playButton setBackgroundImage:[[UIImage imageNamed:@"Image-MVPause"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [OnlineAVplay shareAVPlayer].isPlay = YES;
    }
}

- (void)MVTableViewBuild {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, high / 2, wide, high / 2) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerClass:[XCShareMusicTableViewCell class] forCellReuseIdentifier:@"XCShareMusicTableViewCell"];
    
    [self.view addSubview:self.tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XCShareMusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCShareMusicTableViewCell"];
    cell.xcnewMVModel = self.dataSource[indexPath.row];
    cell.numberLable.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    cell.buttonImageView.image = [[UIImage imageNamed:@"Image-MMVV"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    XCNewMVModel *newMVModel = self.dataSource[indexPath.row];
    XCNewMVListModel *MVListModel = newMVModel.mvList[0];
    [[OnlineAVplay shareAVPlayer] playWithUrl:MVListModel.url];
    [[OnlineAVplay shareAVPlayer] play];
    [OnlineAVplay shareAVPlayer].isPlay = YES;
    [self.playButton setBackgroundImage:[[UIImage imageNamed:@"Image-MVPause"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return wide / 5;
}

- (void)leftBarButtonItemAction {
//    [[OnlineAVplay shareAVPlayer] pause];
//    [OnlineAVplay shareAVPlayer].isPlay = NO;
    [self.playButton setBackgroundImage:[[UIImage imageNamed:@"Image-MVPlay"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)changeBigButtonAction {
    
    if (_isChangeBig == YES) {
        
        self.playerView.transform = CGAffineTransformIdentity;
        self.playerView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 2);
        self.playerLayer.frame = CGRectMake(0, 0, _playerView.bounds.size.width, _playerView.bounds.size.height);
        self.slider.frame = CGRectMake(50, _playerView.bounds.size.height - 40, _playerView.bounds.size.width - 100, 30);
        self.playButton.frame = CGRectMake(10, _playerView.bounds.size.height - 40, 30, 30);
        
        self.changerBigButton.frame = CGRectMake(_playerView.bounds.size.width - 40, _playerView.bounds.size.height - 40, 30, 30);
        [self.changerBigButton setBackgroundImage:[[UIImage imageNamed:@"Image-changeBig"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        
        self.tableView.hidden = NO;
        self.isChangeBig = NO;
        
    } else {
        
        self.playerView.transform = CGAffineTransformMakeRotation(M_PI / 2);
        self.playerView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        self.playerView.backgroundColor = [UIColor blackColor];
        _playerLayer.frame = CGRectMake(_playerView.bounds.origin.x, _playerView.bounds.origin.y, _playerView.bounds.size.width, _playerView.bounds.size.height - 40);
        [self.changerBigButton setBackgroundImage:[[UIImage imageNamed:@"Image-changeSmall"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        
        self.slider.frame = CGRectMake(50, _playerView.bounds.size.height - 40, _playerView.bounds.size.width - 100, 30);
        self.playButton.frame = CGRectMake(10, _playerView.bounds.size.height - 40, 30, 30);
        
        self.changerBigButton.frame = CGRectMake(_playerView.bounds.size.width - 40, _playerView.bounds.size.height - 40, 30, 30);
        
        self.isChangeBig = YES;
        
        self.tableView.hidden = YES;
    }
}
- (void)viewWillDisappear:(BOOL)animated {
    [[OnlineAVplay shareAVPlayer] pause];
    [OnlineAVplay shareAVPlayer].isPlay = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
